import { createSlice} from '@reduxjs/toolkit';
const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    count: 0,
  },
  reducers: {
    increment: (state) => {
      state.count += 1;
    },
    decrement: (state) => {
      state.count -= 1;
    },
    // 下面这个方法就是让传参也参与到结果中
    incrementByAmount: (state, action) => {
      state.count += action.payload;
    },
  },
});

// 其中actions方法是可以解构出actionCreator出来的，这样就相当于是可以提取出里面的方法，然后在组件中直接调用，而不需要再去import actionCreator
//最后那句话就相当于下面两句话的合并：要学会这种简化合并写法
// const {increment, decrement} = counterSlice.actions;
//export {increment, decrement}
export const { increment, decrement, incrementByAmount } = counterSlice.actions;

// 注意若是下面的这种写法，即前面有个 default 的时候，那这种写法就是错误的了
// export default const counterReducer = counterSlice.reducer;
const counterReducer = counterSlice.reducer;
export default counterReducer;